<?php

namespace app\shop\controller;

use app\shop\model\ShopGoods;
use app\shop\model\ShopStoreGoodsStock;
use buildView\Filter;
use think\Controller;
use buildView\Detail;
use buildView\Form;
use buildView\Grid;

/**
 * 出入库记录
 * Class StoreStock
 * @package app\shop\controller
 */
class StoreStock extends Controller
{
    /**
     * 列表
     * @auth true
     * @menu true
     * @return string
     */
    public function index()
    {
        $grid = new Grid(new ShopStoreGoodsStock());
        $grid->goods()->title('商品名称');
        $grid->goods_spec('商品规格')->display(function ($val) {
            $res = json_decode($val, true);
            $html = '';
            foreach ($res as $value) {
                $html .= "{$value['spec_name']} <br>数量：<span class='layui-badge'>{$value['num']}</span><br>";
            }
            return $html;
        })->align('left');
        $grid->desc('描述');
        $grid->type('状态')->using([
            1 => '入库',
            2 => '出库'
        ])->badge();
        $grid->hideColumnSelect();
        $grid->hideAction();
        $grid->hideAddButton();
        $grid->hideDeletesButton();
        $grid->create_at('操作时间');
        $grid->filter(function (Filter $filter) {
            $goods = ShopGoods::where('is_deleted', 0)->column('title', 'id');
            $filter->eq('goods_id', '商品名称')->select($goods);
            $filter->dateBetween('create_at','操作时间');
        });
        return $grid->view();
    }

    /**
     * 详情
     * @auth true
     * @menu true
     * @return string
     */
    public function detail()
    {
        $detail = new Detail(new ShopStoreGoodsStock);
        $detail->id('id');
        $detail->goods_id('商品ID');
        $detail->goods_spec('商品规格');
        $detail->desc('描述');
        $detail->type('1入库，2出库');
        $detail->create_at('创建时间');

        return $detail->view();
    }

    /**
     * 添加/编辑
     * @auth true
     * @menu true
     * @return string
     */
    public function form()
    {
        $form = new Form(new ShopStoreGoodsStock);
        $form->number('id', 'id');
        $form->number('goods_id', '商品ID');
        $form->text('goods_spec', '商品规格');
        $form->ckeditor('desc', '描述');
        $form->switch('type', '1入库，2出库');
        $form->datetime('create_at', '创建时间');

        return $form->view();
    }
}
